var ip = 'http://' + document.location.host;

//弹窗返回按钮
$('.closeBtn').on('click',function() {
	location.reload();
	window.parent.document.getElementById("alert_shade").style.display="none";
	window.parent.document.getElementById("edit_produce_box").style.display="none";
});

$(function() {
    initTree();
})

	var setting_ztree = {
	    view: {
	        showLine: true,
	        showIcon:true,
	        selectedMulti : true,
	        dblClickExpand: false,
	        addHoverDom: addHoverDom,
	        removeHoverDom: removeHoverDom
	    },check : {
	        enable : true,
	        chkStyle : "checkbox",
	        radioType : "all"
	    },
	    data: {
	        simpleData: {
	            enable: true
	        }
	    },
	    callback: {
	        //onClick: onClick_ztree,
	        /*onCheck: onClick_ztree*/
	    }
	};
	function addHoverDom(treeId, treeNode) {
	    $("#" + treeNode.tId).addClass("hover");
	};
	function removeHoverDom(treeId, treeNode) {
	    $("#" + treeNode.tId).removeClass("hover");
	};
	function filter(node) {
		var fybm = node.id;
		var fymc = node.name;
		//if((fybm.length==3&&fybm.substr(2,1)!='0'&&(fybm.substr(0,1)=='1'||fybm.substr(0,1)=='2'||fybm.substr(0,1)=='M'||fybm.substr(0,1)=='9'))||fymc.indexOf("上海金融法院")!=-1||fymc.indexOf("矿区")!=-1||fymc.indexOf("知识产权")!=-1||fymc.indexOf("铁路运输")!=-1||fymc.indexOf("人民解放军")!=-1||fymc.indexOf("海事法院")!=-1||(fybm.length==3&&fybm=='000')||(fybm.length==3&&fybm.substring(0,2)=='79')||(fybm.length==3&&fybm.substring(0,2)=='7C')||(fybm.length==3&&fybm.substring(0,2)=='8J')||(fybm.length==3&&fybm.substring(0,2)=='SG')||(fybm.length==3&&fybm.substring(0,2)=='8G')||(fybm.length==3&&fybm.substring(0,2)=='6G')||(fybm.length==3&&fybm.substring(0,2)=='L3')||(fybm.length==3&&fybm.substring(0,2)=='L5')||(fybm.length==3&&fybm.substring(0,2)=='L6')||(fybm.length==3&&fybm.substring(0,2)=='16')||(fybm.length==3&&fybm.substring(0,2)=='93')||(fybm.length==3&&fybm.substring(0,2)=='HD')){//||(fybm.length==3&&fybm.substring(2,3)!='0')||(fybm.length==3&&fybm.substring(2,1)!='0')	
		if(fymc.indexOf("全部")!=-1||fybm.length==4||fymc.indexOf("上海金融法院")!=-1||fymc.indexOf("矿区")!=-1||fymc.indexOf("知识产权")!=-1||fymc.indexOf("铁路运输")!=-1||fymc.indexOf("人民解放军")!=-1||fymc.indexOf("海事法院")!=-1||(fybm.length==3&&fybm.substring(0,2)=='79')||(fybm.length==3&&fybm.substring(0,2)=='7C')||(fybm.length==3&&fybm.substring(0,2)=='8J')||(fybm.length==3&&fybm.substring(0,2)=='SG')||(fybm.length==3&&fybm.substring(0,2)=='8G')||(fybm.length==3&&fybm.substring(0,2)=='6G')||(fybm.length==3&&fybm.substring(0,2)=='L3')||(fybm.length==3&&fybm.substring(0,2)=='L5')||(fybm.length==3&&fybm.substring(0,2)=='L6')||(fybm.length==3&&fybm.substring(0,2)=='16')||(fybm.length==3&&fybm.substring(0,2)=='93')||(fybm.length==3&&fybm.substring(0,2)=='HD')){
			return node;
		}
	} 
	var initTree = function(){
	        $.ajaxSettings.async = false;
	        $.post( "/user/tree", {}, function(response) {
	            var zNodes = response.data;
	            zTreeObj = $.fn.zTree.init($("#partTree"), setting_ztree, zNodes);
	            var nodescheck = zTreeObj.getNodesByFilter(filter);
	            for(var i=0;i<nodescheck.length;i++){
	            	nodescheck[i].nocheck = true;
	            	//nodescheck[i].chkDisabled = true;
	                zTreeObj.updateNode(nodescheck[i]);
	            }
	            var nodes = zTreeObj.getNodes();
	            for(var i=0;i<nodes.length;i++){
	                zTreeObj.expandNode(nodes[i], true, false, false);//默认展开第一级节点
	            }
	            var nodesSys = zTreeObj.getNodes(); //可以获取所有的父节点
	            var nodesSysAll = zTreeObj.transformToArray(nodesSys); //获取树所有节点
	            for (var i = 0;i<nodesSysAll.length;i++){
	                //两种禁用的效果都有效
	                nodesSysAll[i].chkDisabled = true;
	                zTreeObj.updateNode(nodesSysAll[i]);
	            	//zTreeObj.setChkDisabled(nodesSysAll[i], true);
	            }
	        });
	        $.ajaxSettings.async = true;
	}

	
	//用户输入判断
	$('.saveBtn').on('click', function() {
		var id = $('#id').val();
		var title = $('#title').val();
		var type = $('#type').val();
		var zTree = $.fn.zTree.getZTreeObj("partTree");
		var nodes = zTree.getSelectedNodes();
		var fybm = "";
		var fymc = "";
		var timeDimension = "";
		var year = "";
		var zsType = "";
		if(!title){
			layer.tips("名称不能为空！", '#title', {time: 2000,tips: [3,"red"]});
			return false;
		}
		/*if(nodes.length==0){
			layer.tips("所属法院不能为空！", '#partTree', {time: 2000,tips: [3,"red"]});
			return false;
		}else{
			fybm = nodes[0].id;
			fymc = nodes[0].name;
		}*/
		
		if(nodes.length==0){
			layer.tips("所属法院不能为空！", '#partTree', {time: 2000,tips: [3,"red"]});
			return false;
		}else{
			for(var i=0;i<nodes.length;i++){
				if(nodes[i].id!='0'){
					fybm = fybm + ";" + nodes[i].id;
					fymc = fymc + ";" + nodes[i].name;
				}
			}
		}
		
        var formData = new FormData();
        var fileInput = $('#report').get(0).files[0];
        /*if(!fileInput){
        	layer.tips("请上传报告！", '#report', {time: 2000,tips: [3,"red"]});
        	return false;
        }*/
        formData.append("file",fileInput);
        formData.append("title",title);
        formData.append("fybm",fybm);
        formData.append("fymc",fymc);
        formData.append("type",type);
        if(type=='年报'){
        	zsType = $('#zsType').val();
        	year = $('#year').val();
        }else if(type=='半年报'){
        	zsType = $('#zsType').val();
        	year = $('#year').val();
        	timeDimension = $('#half').val();
        }else if(type=='季报'){
        	zsType = $('#zsType').val();
        	year = $('#year').val();
        	timeDimension = $('#quarter').val();
        }else if(type=='月报'){
        	zsType = $('#zsType').val();
        	year = $('#year').val();
        	timeDimension = $('#month').val();
        }
        formData.append("zsType",zsType);
        formData.append("year",year);
        formData.append("timeDimension",timeDimension);
        formData.append("id",id);
        $.ajax({
            type: "POST",
            url: '/report/update',
            data: formData,
            processData: false,
            contentType: false,
            success: function(res) {
	        	if(res.code == 200){
	        		location.reload();
	        		parent.location.reload();
	        	}else{
	        		layer.alert(res.msg, {icon: 5,title: "提示"});
	        	}
	            
	        }
        });
	});